package com.rickpan.repository;

import com.rickpan.entity.VipFeatureConfig;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * VIP功能配置数据访问接口
 */
@Repository
public interface VipFeatureConfigRepository extends JpaRepository<VipFeatureConfig, Long> {

    /**
     * 根据功能代码查找配置
     */
    Optional<VipFeatureConfig> findByFeatureCode(String featureCode);

    /**
     * 查找所有启用的功能配置
     */
    List<VipFeatureConfig> findByIsActiveTrueOrderBySortOrder();

    /**
     * 查找所有功能配置 (按排序顺序)
     */
    List<VipFeatureConfig> findAllByOrderBySortOrder();

    /**
     * 根据限制周期查找功能配置
     */
    List<VipFeatureConfig> findByLimitPeriodAndIsActiveTrueOrderBySortOrder(VipFeatureConfig.LimitPeriod limitPeriod);

    /**
     * 查找有BASIC用户限制的功能
     */
    @Query("SELECT v FROM VipFeatureConfig v WHERE v.basicLimit > 0 AND v.isActive = true ORDER BY v.sortOrder")
    List<VipFeatureConfig> findBasicLimitedFeatures();

    /**
     * 查找VIP专享功能 (BASIC用户限制为-1)
     */
    @Query("SELECT v FROM VipFeatureConfig v WHERE v.basicLimit = -1 AND v.isActive = true ORDER BY v.sortOrder")
    List<VipFeatureConfig> findVipExclusiveFeatures();

    /**
     * 统计启用的功能数量
     */
    @Query("SELECT COUNT(v) FROM VipFeatureConfig v WHERE v.isActive = true")
    Long countActiveFeatures();

    /**
     * 检查功能代码是否存在
     */
    boolean existsByFeatureCode(String featureCode);
}